package org.lq.dlz.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.lq.dlz.dao.UserDao;
import org.lq.dlz.entity.User;
import org.lq.dlz.util.DateUtils;
import org.lq.dlz.util.JDBCUtils;



/**
 * @author 丁苗
 * @组名 狂奔的肉夹馍
 * @version创建时间 2019年6月14日上午9:55:24
 */
public class UserDaoImpl implements UserDao {
   private QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
	
	/**
     * 添加
     */
	@Override
	public int addUser(User user) {
        String sql = "insert into user(username,password,email,"
        		+ "phone,question,answer,role,status,"
        		+ "message,defaultAddress,create_time,update_time)"
        		+ "values(?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			return qr.update(sql, user.getUsername(),user.getPassword(),user.getEmail(),
					user.getPhone(),user.getQuestion(),
					user.getAnswer(),user.getRole(),user.getStatus()
					,user.getMessage(),user.getDefaultAddress(),DateUtils.getTimestamp(),
					DateUtils.getTimestamp());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 删除
	 */
	@Override
	public int deleteUser(int id) {
       String sql="delete from user where id = ?";
       try {
		return qr.update(sql, id);
	} catch (SQLException e) {
		e.printStackTrace();
	}
       return 0;
	}

	/**
	 * 查询用户
	 */
	@Override
	public List<User> findUser() {
        String sql ="select * from user";
        try {
			return qr.query(sql, new BeanListHandler<User>(User.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 修改
	 */
	@Override
	public int updateUser(User user) {
        String sql = "update user set username=?,password=?,email=?,"
        		+ "phone=?,question=?,answer=?,role=?,status=?,message=?"
        		+ ",defaultAddress=?,update_time=? where id =?";
        try {
			return qr.update(sql, user.getUsername(),user.getPassword(),user.getEmail(),
					user.getPhone(),user.getQuestion(),
					user.getAnswer(),user.getRole(),user.getStatus()
					,user.getMessage(),user.getDefaultAddress(),
					DateUtils.getTimestamp(),user.getId());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 登录
	 */
	@Override
	public User login(String username, String password) {
        String sql = "select * from user where username=? and password=?";
        try {
			return qr.query(sql, new BeanHandler<User>(User.class), username,password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/*
	 * 使用用户名查询
	 */
	@Override
	public User getUserByUsername(String username) {
		 String sql = "select * from user where username=?";
	        try {
				return qr.query(sql, new BeanHandler<User>(User.class), username);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
	}

	/**
	 * 修改状态
	 */
	@Override
	public int updateStatus(int status, int id) {
        String sql = "update user set status=? where id=?";
        try {
			return qr.update(sql, status,id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 通过邮箱查询
	 */
	@Override
	public User getUserByEmail(String email) {
		 String sql = "select * from user where email=?";
	        try {
				return qr.query(sql, new BeanHandler<User>(User.class), email);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
	}

	/**
	 * 通过id查询
	 */
	@Override
	public User getUserById(int id) {
		 String sql = "select * from user where id=?";
	        try {
				return qr.query(sql, new BeanHandler<User>(User.class), id);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
	}

	/**
	 * 修改密码
	 */
	@Override
	public int updatePwd(String username, String password) {
        String sql = "update user set password=? where username=?";
        try {
			return qr.update(sql, username,password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return 0;
	}

}
